﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
namespace DAO
{
    public static class Phi_DAO
    {
        private static QLTDTVDataContext dt = new QLTDTVDataContext();

        public static List<Phi_DTO> DanhSachPhi()
        {
            var list = dt.spLayDanhSachPhi();
            List<Phi_DTO> dsphi = new List<Phi_DTO>();
            foreach (var p in list)
            {
                Phi_DTO phi = new Phi_DTO();
                phi.GiaVaThoiHan = p.gia + "," + p.ThoiHan;
                phi.Gia = (float)p.gia;
                phi.ThoiHan = (int)p.ThoiHan;
                phi.Ten = p.Ten;
                dsphi.Add(phi);
            }
            return dsphi;
        }

        //new update
        public static Phi_DTO LayThongTinPhiTheoMaPhi(int maPhi)
        {
            var dsPhi = dt.spLayThongTinPhiTheoMaPhi(maPhi);

            Phi_DTO phi = new Phi_DTO();
            foreach (var item in dsPhi)
            {
                phi.Ten = item.Ten;
                phi.MaPhi = item.MaPhi;
                phi.ThoiHan = (int)item.ThoiHan;
                phi.DoiTuong = (int)item.DoiTuong;
                phi.DoUuTien = (int)item.DoUuTien;
                phi.SoLuongTin = (int)item.SoLuongTin;
                phi.Gia = (float)item.Gia;
                phi.ViTriCaoNhat = (int)item.ViTriCaoNhat;
                phi.ViTriThapNhat = (int)item.ViTriThapNhat;
            }

            return phi;
        }

        public static bool CapNhatPhi(Phi_DTO phi)
        {
            try
            {
                string flag = "";
                
                dt.spCapNhatPhi(phi.MaPhi, phi.Ten, phi.ThoiHan, phi.DoiTuong, phi.DoUuTien, phi.SoLuongTin,Convert.ToDecimal(phi.Gia), phi.ViTriCaoNhat, phi.ViTriThapNhat,ref flag);

                if (flag == "1")
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
